package com.losl.framework.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ArrayUtil;
import com.losl.common.alioss.AliOssService;
import com.losl.common.core.domain.Result;
import com.losl.common.core.domain.entity.SysUser;
import com.losl.common.core.domain.model.LoginUser;
import com.losl.common.util.ServletUtils;
import com.losl.framework.web.service.TokenService;
import com.losl.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FileUploadController {
    private final String[] ALLOW_IMAGE_TYPE = {".png", ".jpg", ".jpeg", ".gif", ".bmp"};
    private final String[] ALLOW_FILE_TYPE = {".png", ".jpg", ".gif", ".bmp", ".rar", ".zip", ".tar", ".gz", ".7z",
            ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt"};
    private final String[] ALLOW_VIDEO_TYPE = {".flv", ".swf", ".avi", ".rmvb", ".mpg", ".wmv", ".mp4", ".mp3"};

    @Autowired
    private AliOssService ossService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/upload")
    public Result ueditorUpload(MultipartFile file, String action) throws IOException {
        String prefix = "losl";
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if ("image".equals(action)) {
            prefix = prefix + "/image";
            if (!ArrayUtil.contains(ALLOW_IMAGE_TYPE, suffix)) {
                return Result.error("Not allowed image type");
            }
        } else if ("video".equals(action)) {
            prefix = prefix + "/video";
            if (!ArrayUtil.contains(ALLOW_VIDEO_TYPE, suffix)) {
                return Result.error("Not allowed video type");
            }
        } else if ("file".equals(action)) {
            prefix = prefix + "/file";
            if (!ArrayUtil.contains(ALLOW_FILE_TYPE, suffix)) {
                return Result.error("Not allowed file type");
            }
        }

        String url = upload(file, prefix);
        Result result = Result.success("操作成功");
        result.put("size",file.getSize());
        result.put("original",file.getOriginalFilename());
        result.put("url",url);
        return result;
    }

    @PostMapping("/profile/upload")
    public Result uploadProfile(MultipartFile file) throws IOException {
        String prefix="profile";
        String profilePath = upload(file, prefix);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser sysUser = new SysUser();
        sysUser.setUserId(loginUser.getUser().getUserId());
        sysUser.setProfile(profilePath);
        userService.updateById(sysUser);
        SysUser userUpdate = userService.getById(sysUser.getUserId());
        loginUser.setUser(userUpdate);
        tokenService.refreshToken(loginUser);
        return Result.success().put("url",profilePath);
    }


    public String upload(MultipartFile file, String prefix) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String extName = "";
        int index = originalFilename.lastIndexOf(".");
        extName = originalFilename.substring(index + 1);
        String filePath = prefix + "/" + DateUtil.today() + "/" + UUID.fastUUID().toString() + "." + extName;
        ossService.putFile(file.getInputStream(), filePath);
        return ossService.getUrlPrefix()+filePath;
    }


}
